<template>
    <div class="profileChart" id="profileChart">
    </div>
</template>

<script>
    import echarts from "echarts";
    import ecStat from "echarts-stat";
    export default {
        name: "profileChart",
        props: ["profileData"],
        data(){
            return {
                profileName: "沉降拟合曲线",
                profileChart: null,
                profileData1: [
                    [96.24, 11.35],
                    [33.09, 85.11],
                    [57.60, 36.61],
        ],
            }
        },
        watch: {
            profileData: {
                handler(newdata, olddata) {
                    //console.log(newdata);
                    this.profileData1 = newdata;
                    this.drawLineChart();
                },
            }
        },
        mounted(){
            this.drawLineChart();
        },
        methods:{
            drawLineChart() {
                this.profileChart = echarts.init(document.getElementById("profileChart"));
                var _this = this;
                var myRegression = ecStat.regression('polynomial', this.profileData1, 3);
                myRegression.points.sort(function(a, b) {
                    return a[0] - b[0];
                });

                var option = {

                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross'
                        }
                    },
                    title: {
                        text: this.profileName,
                        left: 'center',
                        top: 2
                    },
                    xAxis: {
                        type: 'value',
                        splitLine: {
                            lineStyle: {
                                type: 'dashed'
                            }
                        },
                        splitNumber: 20
                    },
                    yAxis: {
                        type: 'value',
                       // min: -10,
                        splitLine: {
                            lineStyle: {
                                type: 'dashed'
                            }
                        }
                    },
                    grid: {
                        top: 50
                    },
                    series: [{
                        name: 'scatter',
                        type: 'scatter',
                        label: {
                            emphasis: {
                                show: true,
                                position: 'right',
                                textStyle: {
                                    color: 'blue',
                                    fontSize: 16
                                }
                            }
                        },
                        data: this.profileData1
                    }, {
                        name: 'line',
                        type: 'line',
                        smooth: false,
                        showSymbol: false,
                        data: myRegression.points,
                        markPoint: {
                            itemStyle: {
                                normal: {
                                    color: 'transparent'
                                }
                            },
                            label: {
                                normal: {
                                    show: true,
                                    position: 'left',
                                    formatter: myRegression.expression,
                                    textStyle: {
                                        color: '#333',
                                        fontSize: 14
                                    }
                                }
                            },
                            data: [{
                                coord: myRegression.points[myRegression.points.length - 1]
                            }]
                        }
                    }]
                };;
                if (option && typeof option === "object") {
                    this.profileChart.setOption(option, true);
                }
                window.addEventListener('resize', function() {
                    _this.profileChart.resize() //初始化的
                } )
            }
        }
    }
</script>

<style scoped>
    .profileChart{
        height:280px
    ;
    }

</style>
